<?php
if( isset($_POST['save']) )
{
    $query_user = $db->query( 'SELECT id from users where name = "'.$_SESSION['user'].'"' );
    $name = $query_user->fetch();    
    
    $rest_user = $db->query( 'SELECT id from restaurants where rname = "'.$_POST['rest'].'"' );
    $rest = $rest_user->fetch();
    
    $db->query( 'INSERT INTO orders (user, restaurant, date, location, status, price) VALUES ( '.$name['id'].', '.$rest['id'].', '.time().', "'.$_SESSION['location'].'", 0, '.$_SESSION['basket']['suma'].')' );
    $orderID = $db->lastInsertId();
    
    foreach( $_SESSION['basket'][$_POST['rest']] as $item => $count)
    {
        $db->query( 'INSERT INTO ordermeals (`order`, menuPosition, count) VALUES ( '.$orderID.', '.$item.', '.$count.')' );
    }
    
    echo '<h2>Zamówienie zostało dodane do realizacji ;)</h2>';
    return true;
}
?>

<div class="container">
    <table id="podsumowanie">
        <tr>
            <td><b>Restauracja</b></td>
            <td><?php echo $_POST['rest']; ?></td>
        </tr>        
        <tr>
            <td><b>Suma do zapłaty</b></td>
            <td><?php echo number_format($_SESSION['basket']['suma']/100, 2, '.', ''); ?> zł</td>
        </tr>
        <tr>
            <td><b>Sposób płatności</b></td>
            <td>
                <input type="radio" name="platnosc" disabled="disabled"/> Płatność online<br/>
                <input type="radio" name="platnosc"/> Przy odbiorze
            </td>
        </tr>
        <tr>
            <td><b>Adres</b></td>
            <td><?php echo $_SESSION['location']?> <input type="number" style="width: 50px; margin-left: 20px"/></td>
        </tr>
        <tr>
            <td olspan="2"><b>Dodatkowe informacje</b></td>
        </tr>        
        <tr>
            <td colspan="2">
            <textarea style="width: 90%; height: 200px"></textarea>
            </td>
        </tr>
        
        <tr>
            <td colspan="2" style="text-align: center">
                <form action="?podsumowanie" method="POST">
                    <input type="hidden" name="rest" value="<?php echo $_POST['rest']; ?>"/>
                    <input type="hidden" name="save" value="1"/>
                    
                    <input type="submit" value="Potwierdź zamówienie"/>
                </form>
            </td>
        </tr>
    </table>
</div>